package kku.cs.fgl;

import java.util.Enumeration;
import java.util.Vector;

import org.newdawn.slick.Color;
import org.newdawn.slick.Graphics;

import kku.cs.fgl.SpriteGroup.SFrame;

public class SpriteGroup {
	private int delay = -1;
	private int x, y;
	private Vector<SFrame> frames = new Vector<SFrame>();

	public class SFrame {
		SpriteCell cell;
		int x, y;
		boolean flip_h = false, flip_v = false;
		int delay = -1;

		public SFrame(SpriteCell cell, double x, double y, boolean flip_h,
				boolean flip_v, int delay) {
			super();
			this.cell = cell;
			this.x = (int) x;
			this.y = (int) y;
			this.flip_h = flip_h;
			this.flip_v = flip_v;
			this.delay = delay;
		}

		public void paint(Graphics g, int x, int y, Color filter) {
			x += this.x + SpriteGroup.this.x;
			y += this.y + SpriteGroup.this.y;
			cell.paint(g, x, y, flip_h, flip_v, filter);
		}

		public void paint(Graphics g, int x, int y, boolean hflip,
				boolean vflip, Color filter) {
			x += this.x + SpriteGroup.this.x;
			y += this.y + SpriteGroup.this.y;
			if (flip_h)
				hflip = !hflip;
			if (flip_v)
				vflip = !vflip;
			cell.paint(g, x, y, hflip, vflip, filter);
		}

		public SpriteCell getCell() {
			return cell;
		}

		public int getX() {
			return x+SFrame.this.x;
		}

		public int getY() {
			return y+SFrame.this.y;
		}

		public boolean isFlip_h() {
			return flip_h;
		}

		public boolean isFlip_v() {
			return flip_v;
		}

		public int getDelay() {
			if(delay<0)return SpriteGroup.this.delay;
			return delay;
		}

		public int getWidth() {
			return cell.getWidth();
		}

		public int getHeight() {
			return cell.getHeight();
		}

		public Color getPixel(int x, int y) {
			return cell.getPixel(x-getX(), y-getY());
		}
		
		
	}

	public SFrame add(SpriteCell cell, float x, float y, boolean flip_h,
			boolean flip_v, int delay) {
		SFrame frame = new SFrame(cell, x, y, flip_h, flip_v, delay);
		frames.add(frame);
		return frame;
	}
	public void add(SpriteCell ... cells) {
		for(SpriteCell c:cells){
		  frames.add(new SFrame(c,0,0,false,false,-1));		  
		}
	}

	public Enumeration<SFrame> elements() {
		return frames.elements();
	}

	public SFrame get(int index) {
		return frames.get(index);
	}

	public int size() {
		return frames.size();
	}

	public SFrame[] array() {
		return frames.toArray(new SFrame[size()]);
	}

	public int getDelay() {
		return delay;
	}

	public void setDelay(int delay) {
		this.delay = delay;
	}

	public int getX() {
		return x;
	}

	public void setX(int x) {
		this.x = x;
	}

	public int getY() {
		return y;
	}

	public void setY(int y) {
		this.y = y;
	}
}
